/*
  学习目标：ref获取组件实例对象
*/

import React, { Component } from 'react';

export default class App extends Component {
  // 1. 创建ref对象
  childRef = React.createRef();

  render() {
    return (
      <div>
        {/* 2. 绑定给组件标签 */}

        <Child ref={this.childRef}></Child>

        <button
          onClick={() => {
            // 3. ref对象.current属性获取
            console.log(
              'this.childRef.current  ----->  ',
              this.childRef.current.state.count
            );

            this.childRef.current.handleAlert();
          }}
        >
          点我获取子组件的实例对象
        </button>
      </div>
    );
  }
}

// 🔔 函数式组件没有实例对象，早期无法通过ref获取函数式组件实例对象
class Child extends Component {
  state = {
    count: 99,
  };

  handleAlert() {
    alert('你调用了Child子组件的方法');
  }
  render() {
    return <div></div>;
  }
}
